package com.ymttest.database.mongo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;

public class JMQMessageDetailWapper {
	MongoDBDriver mdd;

	public JMQMessageDetailWapper() throws Exception {
		
		}

	/**
	 * 用uid查询AccessToken
	 * 
	 * @param uid
	 * @return AccessToken
	 * @throws Exception
	 */
	public JMQMessageDetailBean getMessageDetailByMid(String appName,String codeName,String mid) throws Exception {
		
		SimpleDateFormat   sDateFormat   =   new   SimpleDateFormat("yyyyMM");   
		String   date   =   sDateFormat.format(new   java.util.Date());  
	
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set5_mongoip_noauth"), Integer.valueOf(EnvSetup
				.getSystemUrl("set5_mongoport_noauth")), "JMQ_V2_Message_"+appName+"_"+date);

		mdd.selectCollection("Message_"+codeName+"_detail");
	
		String re = null;
		JMQMessageDetailBean mb = null;
		ArrayList<String> list = mdd.selectPartLimit("{\"bizId\":\"" + mid + "\"}", 1);
		try {
			if (list.size() > 0) {
				re = list.get(0);
				mb = (JMQMessageDetailBean) FormatUtil.GSONToObject(re, JMQMessageDetailBean.class);
				return mb;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void saveData(String appName,String codeName,String json) throws NumberFormatException, Exception
	{
		SimpleDateFormat   sDateFormat   =   new   SimpleDateFormat("yyyyMM");   
		String   date   =   sDateFormat.format(new   java.util.Date());  
		
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set5_mongoip_noauth"), Integer.valueOf(EnvSetup
				.getSystemUrl("set5_mongoport_noauth")), "JMQ_V2_Message_"+appName+"_"+date);
		mdd.selectCollection("Message_"+codeName+"_detail");
		
		mdd.saveJSON(json);
	}

	public static void main(String args[])
	{
		try {
			JMQMessageDetailBean MessageBean =new JMQMessageDetailWapper().getMessageDetailByMid("testdashen","publishok","0760430153");
			Logger.comment(MessageBean.getConsumerId());
//			Logger.comment(MessageBean.getCtime().toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
